package leetcode.editor.cn;

import java.util.HashSet;
import java.util.Set;

//Java：无重复字符的最长子串
public class LongestSubstringWithoutRepeatingCharacters{
    public static void main(String[] args) {
        Solution solution = new LongestSubstringWithoutRepeatingCharacters().new Solution();
        // TO TEST
    }
    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int lengthOfLongestSubstring(String s) {
            int length = s.length();
            Set<Character> set = new HashSet<>();
            int maxLength = 0;
            int left = 0, right = 0;
            while (left < length && right < length) {
                if (set.add(s.charAt(right))) {
                    right++;
                    maxLength = Math.max(maxLength, right - left);
                } else {
                    set.remove(s.charAt(left));
                    left++;
                }
            }
            return maxLength;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)
}